
import UIKit
#if canImport(TCC_String)
import TCC_String
#endif
#if canImport(TCC_Extension)
import TCC_Extension
#endif
#if canImport(TCC_ViewController)
import TCC_ViewController
#endif
@objcMembers open class CCAlertViewController: UIViewController {
    
    open override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    
    ///Objective-C 使用动态运行时，默认会调用 initWithNibName:bundle: 初始化方法，即使你不明确指定，它也能自动找到并加载对应的 XIB 文件。
    /// Swift 是静态语言，构造函数必须明确调用。它不会自动继承和调用 init(nibName:bundle:)，除非你手动指定，所以 UIViewController() 默认不会加载 XIB 文件。
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        self.view.layoutIfNeeded()
        ///必须要加上这个方法，否则self.modalPresentationStyle = .overFullScreen不生效
        modalPresentationStyle = .overFullScreen
        view.backgroundColor = "#000000".color(0.6)
    }
    private init(){
        super.init(nibName: "MyViewController", bundle: nil)
    }
    // Custom initializer
    public init(useXib: Bool) {
        if useXib {
            super.init(nibName: Self.className, bundle: nil)
            modalPresentationStyle = .overFullScreen
            view.backgroundColor = "#000000".color(0.6)
            ///方法1：默认情况下，self.view 只有在 viewDidLoad 之后才会被加载
            ///手动调用视图加载过程来解决这个问题。可以手动加载视图
            self.loadViewIfNeeded()
        }else{
            super.init(nibName: nil, bundle: nil)
        }
    }
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        view.layoutIfNeeded()
    }
   open func initUI(){}
    
    open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let view = self.firstResponder(self.view)
        if view != nil{
            super.touchesEnded(touches, with: event)
            self.view.endEditing(true)
        } else{
            let touch = (touches as NSSet).allObjects.last as! UITouch
            if touch.view!.isDescendant(of: self.view.subviews.first ?? UIView()) == false && self.currentVC() == self
            {
                self.dismiss(animated:true, completion: nil)
            }
        }
    }
    
}
